package com.example.myx5.app;


import static com.example.myx5.app.App.APP_HOME;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myx5.HomeActivity;
import com.example.myx5.R;
import com.example.myx5.utils.SPUtils;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.JsonMap;
import com.kongzue.baseokhttp.util.Parameter;

public class StartActivity extends AppCompatActivity {

    private String token ;
    private ImageView iv;
    private String webvs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        webvs = (String) SPUtils.get(StartActivity.this,"webversion","0");

        initToken();

    }

    private void initToken() {

        HttpRequest.POST(StartActivity.this, APP_HOME+"/api/auth/token?appid=kwapp", null, new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {

                if (error == null) {
                    Log.d("respione",response+"");
//                    {"code":1,"msg":"","time":1595944972,"data":"88bb4d3121fbea9197b9b73d54e02921"}
                    JsonMap data = JsonMap.parse(response);

                    if(data.getInt("code")==1){
                        token = data.getString("data");
                        Log.d("respione",token+"");
                        SPUtils.put(StartActivity.this,"token",token);
                        initWeb();
                    }else{
                        Toast.makeText(StartActivity.this, data.getString("msg"), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Log.d("respione",error.toString()+"");
                }
            }
        });

    }

    private void initWeb(){

//        /api/app/url
//
        HttpRequest.POST(StartActivity.this, APP_HOME+"/api/app/url",
                new Parameter().add("access_token", token), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {

                if (error == null) {
                    Log.d("respione111111",response+"");
//                    {"code":1,"msg":"","time":1595944972,"data":"88bb4d3121fbea9197b9b73d54e02921"}

                    JsonMap data = JsonMap.parse(response);
                    if(data.getInt("code")==1){ JsonMap dValue = data.getJsonMap("data");
                    SPUtils.put(StartActivity.this,"url",dValue.getString("url"));
                    SPUtils.put(StartActivity.this,"version",dValue.getString("version"));
                    Intent intent = new Intent(StartActivity.this, HomeActivity.class);

                    if(!webvs.equals(dValue.getString("version"))&&!webvs.equals("0")){
                        intent.putExtra("canCLean","1");
                        SPUtils.put(StartActivity.this,"version",dValue.getString("version"));
                    }else{
                        intent.putExtra("canCLean","0");
                        SPUtils.put(StartActivity.this,"version",dValue.getString("version"));
                    }

                    startActivity(intent);
                    finish();
                    }else{

                        Toast.makeText(StartActivity.this, data.getString("msg"), Toast.LENGTH_SHORT).show();

                    }
                } else {
                    Log.d("respione",error.toString()+"");
                }
            }
        });

    }

}
